
import javax.microedition.lcdui.Canvas;

class Dynamic
{
	static final int J_HEIGHT 						= 160;
	int image_index;
	int frame_index;

	static int tx,ty,i,j;

	int x,y;
	int dx,dy;
	int fw,fh;

	// animation variables

	int b[] = null;
	int a1;
	int a2;

//#if Nokia
	Frame frm[] = null;
//#else
	Frame frm_up[] = null;
	Frame frm_dn[] = null;
//#endif

	int frm_disp;

	int aa,v,s,p;

	static int y_points[] = {0, 4, 3};

	int max_period,period,delay_index;

	boolean finished;
	boolean shootEnabled;
	boolean pressed, processed;
	int man_index;
	boolean isGhost;

	int task,time,taskInterval;

	int state = -1,state_delay;

	boolean result;

	int track_x_cur,track_y_cur;		// coords of left edge of current track part, accumulative
	int rx,ry;									// relative coords of man (of current track part)

    int target_y;
    int aim_n;
    int aim_x;
    int aim_y;
    int aim_x_range;
    int aim_amplitude;
    int aim_dx;
    int aim_dy;
    int targets[];
    int s_v;
    int heart_delay;
    int heart_index;
	int missed;

	int start_x, start_y;
	boolean begin = true;

	int w_action;			// store wrong keypress (not in time)
	int action;
	int prev_action;
	int mistake_cnt;

	public Dynamic(){}

	void init(int x,int y,String frame_name,boolean isGhost)
	{
		this.x = start_x = x;
		this.y = start_y = y;
		
		this.isGhost = isGhost;

		if (frame_name != null)
		{
//#if Nokia
			frm = new Frame[6];
			for (i = 0; i < 3; i++)
			{
				frm[i + i] = Main.gf(frame_name,i,0,0);
				frm[i + i + 1] = Main.gf(frame_name,i,1,0);
			}
//#else
			frm_up = new Frame[15];
			frm_dn = new Frame[15];
			for (i = 0; i < 3; i++)
			{
				for (j = 0; j < 5; j++)
				{
					frm_up[i * 5 + j] = Main.gf(frame_name,i,0,j);
					frm_dn[i * 5 + j] = Main.gf(frame_name,i,1,j);
				}
			}
//#endif
		}
		restart();
	}

	void restart()
	{
		x = start_x;
		y = start_y;
		dx = dy = 0;
		begin = true;
		delay_index = 0;
		task = 0;
		time = 0;
		taskInterval = 75;
		state_delay = 0;
		max_period = 4;
		delay_index = 0;
		finished = false;
		shootEnabled = true;
		pressed = false;
		processed = false;
		finished = false;
		frame_index = 0;
		Main.rounds_passed = 0;
		shootEnabled = true;
		result = false;
		aa = v = p = rx = ry = track_x_cur = track_y_cur = man_index = period = 0;
		a1 = 0;
		a2 = 1;
		aim_n = 0;
		aim_x = 0;
		aim_y = 0;
		aim_x_range = 0;
		aim_amplitude = 0;
		aim_dx = 0;
		aim_dy = 0;
		targets = null;
		s_v = 0;
		heart_delay = 0;
		heart_index = 0;
		missed = 0;
		w_action = 0;
		action = 0;
		prev_action = 0;
		mistake_cnt = 0;

//#if Nokia
		if (frm != null)
		{
//#else
		if (frm_up != null)
		{
//#endif
			s = x << 5;
		}
		else
		{
			// here we assume init for SJ
			j_state = 0;
			j_x = 12; 
			j_s = j_x << 5;
			j_y = Main.start_y + 8;
			j_v = 0;
			j_i1 = j_i2 = 2;
			j_t1 = j_t2 = 0;
			j_wind = j_wind_dest = j_wind_power = j_wind_power_dest = 0;
			track_x_sum = Main.track_x[0];
		}
	}

	void next()
	{
		b = Main.anims[a1];
		if (++a2 >= b.length) a2 = b.length - 1;
	}

    void drawBSS()
    {
		image_index = frame_index = 0;
//		if (frames_up != null)
		{
			b = Main.anims[a1];
			if (b != null)
			{
				frame_index = b[0];
				image_index = b[a2];
			}
//#if Nokia
			frm_disp = frame_index * 2;

			if (frm != null)
			{
				fw = frm[frm_disp].fw;
				fh = frm[frm_disp].fh;
				if (frm[frm_disp + 1] != null) fh += frm[frm_disp + 1].fh;

				tx = x - Main.a_x_d - (fw >> 1);
				ty = Main.start_y - y - Main.a_y_d - fh + y_points[delay_index];
			
	  			if ((tx > - fw) && (ty > - fh) && (tx < Main.s_w) && (ty < Main.s_h))
		  		{
		  			Main.setClip(tx,ty,fw,fh);
	  				Main.drawImage(frm[frm_disp].index,tx - image_index,ty);
	  				if (frm[frm_disp + 1] != null)
		  				Main.drawImage(frm[frm_disp + 1].index,tx - image_index,ty + frm[frm_disp].fh);
				}
	  		}
//#else
			frm_disp = frame_index * 5;

			if (frm_up != null)
			{
				fw = frm_up[frm_disp].fw;
				fh = frm_up[frm_disp].fh;
				if (frm_dn[frm_disp] != null) fh += frm_dn[frm_disp].fh;

				tx = x - Main.a_x_d - (fw >> 1);
				ty = Main.start_y - y - Main.a_y_d - fh + y_points[delay_index];
			
	  			if ((tx > - fw) && (ty > - fh) && (tx < Main.s_w) && (ty < Main.s_h))
		  		{
		  			frm_up[frm_disp + image_index].draw(tx,ty);
	  				if (frm_dn[frm_disp + image_index] != null) 
	  					frm_dn[frm_disp + image_index].draw(tx,ty + frm_up[frm_disp + image_index].fh);
				}
	  		}
//#endif
		}
    }
	/////////////////////////////////////////////////////////////////////////////////////////////
	boolean update_skater_run(int param_action)
	{
		action = param_action;

    	aa = - 4;
		if (!finished)
		{
	    	aa = - 6;
		}
		max_period = 4;

		processed = false;
//		mistake_cnt = 0;

		if (action == prev_action)
		{
			action = 0;
		}
		else
		{
			pressed = false;
			prev_action = action;
		}

		if (w_action != 0 && (mistake_cnt == 0))
		{
			if (period != 0) action = 0;
			else
			{
				action = w_action;
				pressed = false;
				w_action = 0;
			}
		}

		switch (action)
	    {
            case -3:
			case Canvas.KEY_NUM4:
					if (!pressed && (!finished))
					{
						if (period == 0)
						{
							if (man_index != 0 || begin)
							{
								man_index = 0;
								pressed = processed = true;
							}
						}								
						else
						{
							mistake_cnt = 1;
							processed = pressed = true;
							w_action = action;
						}
					}
					break;
			case -4:
			case Canvas.KEY_NUM6:
					if (!pressed && (!finished))
					{
						if (period == 0)
						{
							if (man_index != 1 || begin)
							{
								man_index = 1;
								pressed = processed = true;
							}
						}
						else
						{
							mistake_cnt = 1;
							processed = pressed = true;
							w_action = action;
						}
					}
					break;
			case -7:
			case Canvas.KEY_STAR:
					processed = true;
					Main.pause();
					break;
			case Main.NO_ACTION:;
//					pressed = false;
		}
//		pressed = false;
    	dx = 0;

    	if (mistake_cnt > 0)
    	{
    		mistake_cnt--;
    	}
    	else
    	{
			if (processed)
			{
				period = max_period;
				a2 = 1;
				if (begin)
				{
					a2 = 2;
					begin = false;
				}
	   			a1 = man_index + 1;
			}
			//
	    	if (finished) next();
    		else
	    	{
		        if (period > 0)
	    		{
   	    		   	aa = 3;
    		       	while(task <= time)
		           	{
		            	next();
    			       	period--;
    			       	taskInterval = 100 + (v >> 2);
		           		task += taskInterval;
	    	   	    }
	    	        time += 75;
				}
			}
		}

		v += aa;
		if (v < 0) v = 0;
		if (v > 160) v = 160;

		s += v;

		dx = x;
		x = s >> 5;
		dx = x - dx;

    	if (x > Main.track_width)
    	{
    		if (!finished)
    		{
    			a1 = 3;
    			a2 = 1;
    			if (!isGhost) Main.finish_skater_run();
    		}
    		finished = true;
    	}
        return processed;
	}

	boolean update_biathlon(int action)
	{
		switch(state)
		{
			case Main.STATE_BIATHLON_RUN:
				result = update_biathlon_run(action);				
				break;
			case Main.STATE_BIATHLON_SHOOT:
				result = update_biathlon_shoot(action);
				break;
			case Main.STATE_BIATHLON_SHOOT_FINISH:
				result = update_biathlon_shoot(action);
				if (--state_delay <= 0)
				{
					v = 100;
					man_index = 1;
					state = Main.STATE_BIATHLON_RUN;
					if (!isGhost)
					{
						Main.a_x_d = x + 20;	// to provide to appear from left side after shots
					}
				}
				break;
			case Main.STATE_BIATHLON_RUN_FINISH:
				result = update_biathlon_run(action);
				if (--state_delay <= 0)
				{
					if (isGhost)
					{
						state_delay = 0;
					}
					else
					{
						Main.go_delay = 0;
						Main.state_delay = 40;
						state = Main.state = Main.STATE_BIATHLON_RUN_SCORES;
					}
				}
		}
		return result;
	}

    boolean update_biathlon_run(int param_action)
    {
		action = param_action;

    	aa = - 4;
    	dx = delay_index;

    	delay_index = 0;
    	taskInterval = 120;
    	if (Main.track_y[p] > 0)
    	{
    		aa = - 5;
	    	taskInterval = 110;
    		delay_index = 1;
		}
    	if (Main.track_y[p] < 0)
    	{
    		aa = - 2;
	    	taskInterval = 140;
    		delay_index = 2;
		}
       	taskInterval += v >> 2;

		max_period = 4;

		processed = false;
		if (action == prev_action)
		{
			action = 0;
		}
		else
		{
			pressed = false;
			prev_action = action;
		}

		if (w_action != 0 && (mistake_cnt == 0))
		{
			if (period != 0) action = 0;
			else
			{
				action = w_action;
				pressed = false;
				w_action = 0;
			}
		}

		switch (action)
	    {
        /*
			case Canvas.KEY_NUM1:
					finished = true;
					state = Main.STATE_BIATHLON_RUN_FINISH;
					state_delay = 40;
					Main.finish_biathlon_run();
 					break;
             */
            case -3:
			case Canvas.KEY_NUM4:
					if (!pressed && (delay_index != 2) && !finished)
					{
						if (period == 0)
						{
							if (man_index != 0 || begin)
							{
								man_index = 0;
								pressed = processed = true;
							}
						}								
						else
						{
							mistake_cnt = 2;
							if (period == 1) mistake_cnt = 1;
							processed = pressed = true;
							w_action = action;
						}
					}
					break;
			case -4:
			case Canvas.KEY_NUM6:
					if (!pressed && (delay_index != 2) && !finished)
					{
						if (period == 0)
						{
							if (man_index != 1 || begin)
							{
								man_index = 1;
								pressed = processed = true;
							}
						}
						else
						{
							mistake_cnt = 2;
							if (period == 1) mistake_cnt = 1;
							processed = pressed = true;
							w_action = action;
						}
					}
					break;
			case -2:
			case Canvas.KEY_NUM8:
					if (!pressed && (delay_index == 2) && !finished)
					{
						if (period == 0)
						{
							man_index = 1;
							pressed = processed = true;
						}
						else
						{
							mistake_cnt = 2;
							processed = pressed = true;
							w_action = action;
						}
					}
					break;
			case -7:
			case Canvas.KEY_STAR:
					processed = true;
					Main.pause();
					break;
			case Main.NO_ACTION:
					pressed = false;
					prev_action = 0;
		}

		if (dx != delay_index || begin || processed) 
		{
			if (dx == 2 && !processed) // from landing to horiz
			{
				man_index = 0;
			}
   			a1 = man_index + 1 + (delay_index << 1);
		}

    	if (mistake_cnt > 0)
    	{
    		mistake_cnt--;
    	}
    	else
    	{
			if (processed)
			{
				period = max_period;
				a2 = 1;
				if (begin)
				{
					a2 = 2;
					begin = false;
				}
			}

	       	if (period > 0)
			{
        	   	aa = 2;
        		if(task <= time)
	        	{
    	        	next();
	    	    	period--;
        			task += taskInterval;
		        }
    	        time += 75;
        	}

        }

    	dx = 0;

		v += aa;
		if (v < 0) v = 0;
		if (v > 160) v = 160;

		s += v;

		dx = x;
		x = s >> 5;
		dx = x - dx;
        //////////////////////////////////////////////////////////////////////////////////
		if (x < track_x_cur) 
		{
			p --;
			track_x_cur -= Main.track_x[p];
			track_y_cur -= Main.track_y[p];
		}
		if (x > (track_x_cur + Main.track_x[p]))
		{
			track_x_cur += Main.track_x[p];
			track_y_cur += Main.track_y[p];
			p ++;
		}
		if (p >= Main.track_x.length)
		{
			p = 0;
			Main.rounds_passed++;
			shootEnabled = true;
		}

		if (x + 32 > Main.track_width_one)
		{
			if (!finished)
			{
				finished = true;
				state = Main.STATE_BIATHLON_RUN_FINISH;
				state_delay = 40;
				if (!isGhost) Main.finish_biathlon_run();
			}
		}

		rx = x - track_x_cur;

		if (rx > 100 && (p + 1 >= Main.track_x.length) && shootEnabled)
		{
			s_v = v; 					// store current speed for heartbeat control
			shootEnabled = false;
			aim_n = 0;
			aim_x = -10;
			aim_y = 0;
			aim_dy = 5 + (s_v >> 5);
			aim_amplitude = 30;
			aim_x_range = 20;
			targets = new int[5];
			for (i = 0; i < targets.length; i++) targets[i] = 0;
			heart_delay = (v >> 5) + 1;
			heart_index = 0;
			state = Main.STATE_BIATHLON_SHOOT;
		}

		dy = y;
		y = track_y_cur;
		if (Main.track_y[p] < 0) y -= (rx >> 2);
		if (Main.track_y[p] > 0) y += (rx >> 2);
		dy = y - dy;

		return processed;
    }

    boolean update_biathlon_shoot(int action)
    {
    	aim_dx = 0;

    	processed = false;

		switch (action)
	    {
            case -3:
			case Canvas.KEY_NUM4:	
					if (state == Main.STATE_BIATHLON_SHOOT) aim_dx = -2;
			    	processed = true;
					break;
			case -4:
			case Canvas.KEY_NUM6:
					if (state == Main.STATE_BIATHLON_SHOOT) aim_dx = 2;
			    	processed = true;
					break;
			case -5:
			case -6:
			case Canvas.KEY_NUM5:
					if (state == Main.STATE_BIATHLON_SHOOT)
					{
						if (aim_y < 12 && (aim_y > -12) && (aim_x < 8) && (aim_x > -8))
						{
							targets[aim_n] = -1;
						}
						else
						{
							targets[aim_n] = 6;
							missed++;
						}
						aim_x = -10;
						if (++aim_n >= targets.length)
						{
							state_delay = 20;
							state = Main.STATE_BIATHLON_SHOOT_FINISH;
						}
					}
//					action = Main.NO_ACTION;
			    	processed = true;
					break;
			case Main.NO_ACTION:
					break;
			case -7:
			case Canvas.KEY_STAR:
			    	processed = true;
					Main.pause();
		}

		for (i = 0; i < targets.length; i++) 
			if (targets[i] > 0) targets[i]--;

		if (s_v > 0) s_v--;

		aim_y += aim_dy;
		if (aim_y < -aim_amplitude)
		{
			aim_dy = 5 + (s_v >> 5);
		}
		if (aim_y > aim_amplitude)
		{
			aim_dy = - 5 - (s_v >> 5);
		}

		aim_x += aim_dx;
		if (aim_x < - aim_x_range)
		{
			aim_x = - aim_x_range;
		}
		if (aim_x > aim_x_range)
		{
			aim_x = aim_x_range;
		}

		if (--heart_delay <= 0)
		{
			heart_delay = 6 - (s_v >> 5);
			heart_index = 1 - heart_index;
		}
		return processed;
    }

    int j_state = 0;
    int j_x = 0;
    int j_y = 0;
    int j_jx = 0, j_jxr;
    int j_jy = 0;
    int j_v = 0, j_s = 0, j_vy = 0, j_sy = 0, j_h, j_ay;
    int j_dx = 0;
    int j_dy = 0;
    int j_i1, j_i2, j_t1, j_t2, j_wind, j_wind_power, j_wind_dest, j_wind_power_dest, j_wind_d, jw;
    int ji1,ji2;
	int track_x_sum;

	int j_x0, j_y0;

    void update_skijump_1(int action)
    {
    	processed = false;
    	// random events
    	if (j_state == 13)
    	{
    		if (!isGhost)
    		{
    			i = Main.rnd(256);
	    		j = Main.rnd(256);

				if ((i < 12)  && (j_i1 < 3) && (j_i1 > 0)) { j_i1--; processed = true; }
				if ((i > 244) && (j_i1 > 1) && (j_i1 < 4)) { j_i1++; processed = true; }

				if ((j < 12)  && (j_i2 < 3) && (j_i2 > 0)) { j_i2--; processed = true; }
				if ((j > 244) && (j_i2 > 1) && (j_i2 < 4)) { j_i2++; processed = true; }
			}
			if (j_i1 != 2)
			{
				if (++j_t1 > 8)
				{
					j_t1 = 0;
					Main.points --;
					if ((j_i1 == 0) || (j_i1 == 4)) Main.points --;
				}
			}

			if (j_i2 != 2)
			{   	
				if (++j_t2 > 8)
				{
					j_t2 = 0;
					Main.points --;
					if ((j_i2 == 0) || (j_i2 == 4)) Main.points --;
				}
			}
    	}

    	if (!isGhost)
    	{
    		if (Main.rnd(256) < 10 && (j_wind_power > 0) && (j_wind == j_wind_dest))
    		{
    			j_wind_dest = Main.rnd(16);
    			j_wind_d = j_wind_dest - j_wind;
    			if (j_wind_d > 8 || (j_wind_d < -8)) j_wind_d = - j_wind_d;
    			j_wind_d = j_wind_d > 0 ? 1 : -1;
    			processed = true;
    		}
    		if (Main.rnd(256) < 8 && (j_wind_power == jw))
    		{
    			j_wind_power_dest = Main.rnd(4);
    			jw = j_wind_power_dest * 50 / 3;
    			processed = true;
    		}
    	}

    	ji1 = j_i1;
    	ji2 = j_i2;
	    switch (action)
	    {
			case -3:
			case Canvas.KEY_NUM4:
					if (j_i1 < 4) j_i1++;
					if (j_i1 == 2) j_t1 = 0;
					processed = true;
					break;
			case -4:
			case Canvas.KEY_NUM6:
					if (j_i1 > 0) j_i1--;
					if (j_i1 == 2) j_t1 = 0;
					processed = true;
					break;
			case -1:							
			case Canvas.KEY_NUM2:
					if (j_i2 < 4) j_i2++;
					if (j_i2 == 2) j_t2 = 0;
					processed = true;
					break;
			case -2:
			case Canvas.KEY_NUM8:
					if (j_i2 > 0) j_i2--;
					if (j_i2 == 2) j_t2 = 0;
					processed = true;
					break;
			case -6:
			case -5:
			case Canvas.KEY_NUM5:
					if (j_state == 1)	// start to slide
					{
						j_state = 10;
					}
					else
					if (j_state == 12)  // jump from the ramp
					{
						j_state = 13;
						j_dy = j_y;

						j_jx = j_x; // coords of jump
						j_jy = j_y;

						j_x0 = track_x_sum;
						j_y0 = j_y;

						j_vy = j_v = 0;
						j_sy = j_s = 0;

						j_jxr = track_x_sum;

						j_t1 = j_t2 = 0;

						track_x_sum += Main.track_x[3];
					}
					else
					if (j_state == 13)	// prepare to landing
					{
						j_state = 20;
						if (j_i2 == 4)  // wrong landing - should fall
						{
							j_state = 14;		
							j_sy >>= 1;
						}
						else
						{
							if (j_i2 == 3) // mistake
							{
								Main.points -= 5;		
							}
							j_h = j_y;
							j_dx --;
							j_v = j_dx << 5;

							j_ay += 1024;
						}
					}
					processed = true;
					break;
			case Main.NO_ACTION:break;
			case -7:
			case Canvas.KEY_STAR:
					if (Main.go_index == 2)
					{
						processed = true;
						Main.pause();
					}
		}
	}

	void update_skijump_2()
	{
    	if (j_wind != j_wind_dest)
    	{
    		j_wind += j_wind_d;
    		if (j_wind > 15) j_wind = 0;
    		if (j_wind < 0) j_wind = 15;
    	}
    	if (j_wind_power > jw) j_wind_power--;
    	else
    	if (j_wind_power < jw) j_wind_power++;
    }

    void update_skijump_3()
    {
		j_dy = j_y;
		switch (j_state)
		{
		case 1:
			if (-- j_t1 <= 0)	// starts to slide by itself
			{
				if (!isGhost) Main.points -= 5;
				j_state = 10;
			}
			break;
		case 10: // ramp diagonal
			j_v += 5;
			if (j_v > 160) j_v = 160;

			j_s += j_v;

			j_dx = j_x;
			j_x = j_s >> 5;
			j_dx = j_x - j_dx;

			j_y = Main.start_y + j_x * 34 / 48; 

			if (j_x >= track_x_sum)
			{
				j_state = 11;
				track_x_sum += Main.track_x[1];
			}
			break;
		case 11: // ramp horiz part 1
			j_x += j_dx;
			j_y += (j_dx >> 2); 

			if (j_x >= track_x_sum)
			{
				j_state = 12;
				track_x_sum += Main.track_x[2];
			}
			break;
		case 12: // ramp horiz part 2
			j_x += j_dx;
			if (j_x >= track_x_sum)
			{
				j_x0 = track_x_sum;
				j_y0 = j_y;

				j_state = 14;		// falling!
				track_x_sum += Main.track_x[3];
				j_dy = j_y;

				j_jx = j_x; // coords of jump
				j_jy = j_y;

				j_vy = 0;
				j_sy = 0;

//				y_bound_bottom = s_h - 30;
			}
			break;
		case 13: // fly
		case 14:
		case 20:
			j_s += 8192 + (Main.cos[j_wind] * j_wind_power >> 5);

			j_dx = j_x;
			j_x = j_jx + (j_s / 1600);
			j_dx = j_x - j_dx;

			j_vy += j_ay - (j_vy >> 1) - (Main.sin[j_wind] * j_wind_power >> 5);


			j_sy += j_vy;
			j = j_state == 14 ? 900 : 1800;
			j_y = j_jy + (j_sy / j); 

			y = j_y0 + J_HEIGHT + ((j_x - j_x0) >> 1); // check the surface touch
			if (j_y >= y)
			{
				if ((j_state == 13)	|| (j_state == 14) || (j_y - j_h > 100))// was not prepared - fall down
				{
					j_jx = j_x;
					j_s = j_x << 5;

//					if (j_state == 14) 
					j_v = j_dx << 5;

					j_y = j_jy = y;
					if (!isGhost) Main.y_bound_bottom = Main.s_h * 3 / 4;

					j_state = 18;
				}
				else
				{
					j_dx ++;
					j_v = j_dx << 5;
				
					j_state = 15;
					j_jx = j_x;
					j_y = j_jy = y;

					if (!isGhost)
					{
						Main.distance = (j_x - j_jxr) * 10 / 32;
						Main.total_points = Main.distance + Main.distance + Main.points;
						Main.m_v1 = Main.distance;
						Main.m_v2 = Main.points;
						Main.y_bound_bottom = Main.s_h * 3 / 4;
					}
				}
			}
			break;
		case 15: // landing_diagonal
			j_x += j_dx;
			j_y = j_jy + ((j_x - j_jx) >> 1); 
			if (j_x >= track_x_sum)
			{
				j_state = 16;
				track_x_sum += Main.track_x[4];
				j_s = j_x << 5;
			}
			break;
		case 16:
		case 17: // landing horiz 
			if (j_x < track_x_sum)
			{
				j_v -= 3;
				if ((j_v < 110) && (j_state == 16))
				{
					j_state = 17;
					if (!isGhost)
					{
						Main.go_delay = 40;
						Main.go_index = 3;
					}
				}
				if (j_v < 0) j_v = 0;

				j_s += j_v;

				j_dx = j_x;
				j_x = j_s >> 5;
				j_dx = j_x - j_dx;
			}
			break;
		case 18: // landing_diagonal after falling
			if ((j_x + 18 < track_x_sum) && (j_v > 0))
			{
				j_v -= 4;
				j_s += j_v;
				j_x = j_s >> 5;
				j_y = j_jy + ((j_x - j_jx) >> 1); 
			}
			else
			{
				j_v = 0;

				if (!isGhost)
				if (Main.go_index != 3)
				{
					Main.go_index = 3;
					Main.go_delay = 40;
				}
			}
		}

		j_dy = j_y - j_dy;
    }

    void drawSJ()
    {
    	y = j_y;
    	if (!isGhost) y += 3;
		// drawing ski man

//#if Nokia
		if (j_state <= 10) Main.fSn[0].draw(j_x - Main.a_x_d - 11, y - Main.a_y_d - 24);
		if ((j_state == 11) || (j_state == 12)) Main.fSn[1].draw(j_x - Main.a_x_d - 16, y - Main.a_y_d - 28);
		if (j_state == 13)
		{
			Main.fSn[2].draw(j_x - Main.a_x_d, y - Main.a_y_d - 26,j_i1);
			Main.fSn[3].draw(j_x - Main.a_x_d - 12, y - Main.a_y_d - 8,j_i2);
		}
		if (j_state == 14) 
		{
			Main.fSn[2].draw(j_x - Main.a_x_d, y - Main.a_y_d - 26,0);
			Main.fSn[3].draw(j_x - Main.a_x_d - 12, y - Main.a_y_d - 8,4);
		}
		if ((j_state == 15) || (j_state == 20)) Main.fSn[4].draw(j_x - Main.a_x_d - 20, y - Main.a_y_d - 30);
		if (j_state == 16) Main.fSn[5].draw(j_x - Main.a_x_d - 20, y - Main.a_y_d - 32);
		if (j_state == 17) Main.fSn[6].draw(j_x - Main.a_x_d - 20, y - Main.a_y_d - 32);
		if (j_state == 18) 
		{
			Main.fSn[7].draw(j_x - Main.a_x_d - 20, y - Main.a_y_d - 12);
			Main.fSnow.draw(j_x - Main.a_x_d + 10, y - Main.a_y_d + 10);
		}
//#else
		if (j_state <= 10) Main.fSn[0].draw(j_x - Main.a_x_d - 11, y - Main.a_y_d - 24);
		if ((j_state == 11) || (j_state == 12)) Main.fSn[1].draw(j_x - Main.a_x_d - 16, y - Main.a_y_d - 28);
		if (j_state == 13)
		{
			Main.fSn[2 + j_i1].draw(j_x - Main.a_x_d, y - Main.a_y_d - 26);
			Main.fSn[7 + j_i2].draw(j_x - Main.a_x_d - 12, y - Main.a_y_d - 8);
		}
		if (j_state == 14) 
		{
			Main.fSn[2].draw(j_x - Main.a_x_d, y - Main.a_y_d - 26);
			Main.fSn[11].draw(j_x - Main.a_x_d - 12, y - Main.a_y_d - 8);
		}
		if ((j_state == 15) || (j_state == 20)) Main.fSn[12].draw(j_x - Main.a_x_d - 20, y - Main.a_y_d - 30);
		if (j_state == 16) Main.fSn[13].draw(j_x - Main.a_x_d - 20, y - Main.a_y_d - 32);
		if (j_state == 17) Main.fSn[14].draw(j_x - Main.a_x_d - 20, y - Main.a_y_d - 32);
		if (j_state == 18) 
		{
			Main.fSn[15].draw(j_x - Main.a_x_d - 20, y - Main.a_y_d - 12);
			Main.fSnow.draw(j_x - Main.a_x_d + 10, y - Main.a_y_d + 10);
		}

//#endif
    }
}
